package com.codinghero.example.interview.facebook;

import java.util.Scanner;

public class Balance {
	
	private static int NOT_CALCULATED = -1;
	private static int NO_DATA = -1;
	private static int BALANCE_WEIGHT = 10;
	
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		final int N = s.nextInt();
		s.nextLine();
		int[][] leftBalances = new int[N][N];
		int[][] rightBalances = new int[N][N];
		int[] leftWeight = new int[N];
		int[] rightWeight = new int[N];
		int[] leftSupply = new int[N];
		int[] rightSupply = new int[N];
		
		// init
		init(leftWeight);
		init(leftBalances);
		init(rightWeight);
		init(rightBalances);
		
		// handle input
		for (int i = 0; i < N; i++) {
			// left
			String leftStr = s.nextLine();
			handleInput(leftStr, i, leftBalances, leftWeight);
			// right
			String rightStr = s.nextLine();
			handleInput(rightStr, i, rightBalances, rightWeight);
		}
		
		calculateAll(leftBalances, rightBalances, leftWeight, rightWeight, leftSupply, rightSupply);
		
		// output
		output(leftSupply, rightSupply);
	}

	private static void init(int[] weight) {
		for (int j = 0; j < weight.length; j++) {
			weight[j] = NOT_CALCULATED;
		}
	}

	private static void init(int[][] balances) {
		for (int j = 0; j < balances.length; j++) {
			for (int k = 0; k < balances.length; k++) {
				balances[j][k] = NO_DATA;
			}
		}
	}
	
	private static void handleInput(String str, int index, int[][] balances, int[] weight) {
		String[] strArr = str.split(" ");
		if ("0".equals(strArr[0])) {
			for (int j = 1; j < strArr.length; j++) {
				balances[index][j - 1] = Integer.parseInt(strArr[j]);
			}
		} else {
			weight[index] = 0;
			for (int j = 0; j < strArr.length; j++) {
				weight[index] += Integer.parseInt(strArr[j]);
			}
		}
	}
	
	private static void calculateAll(int[][] leftBalances, int[][] rightBalances, int[] leftWeight, int[] rightWeight, int[] leftSupply, int[] rightSupply) {
		for (int i = 0; i < leftBalances.length; i++) {
			calculate(i, leftBalances, rightBalances, leftWeight, rightWeight, leftSupply, rightSupply);
		}
	}
	
	private static void calculate(int index, int[][] leftBalances, int[][] rightBalances, int[] leftWeight, int[] rightWeight, int[] leftSupply, int[] rightSupply) {
		if (leftWeight[index] == NOT_CALCULATED) {
			leftWeight[index] = 0;
			for (int j = 0; j < leftBalances[index].length; j++) {
				if (leftBalances[index][j] == NO_DATA)
					break;
				int currentIndex = leftBalances[index][j];
				if (leftWeight[currentIndex] == NOT_CALCULATED)
					calculate(currentIndex, leftBalances, rightBalances, leftWeight, rightWeight, leftSupply, rightSupply);
				if (rightWeight[currentIndex] == NOT_CALCULATED)
					calculate(currentIndex, leftBalances, rightBalances, leftWeight, rightWeight, leftSupply, rightSupply);
				leftWeight[index] += leftWeight[currentIndex];
				leftWeight[index] += rightWeight[currentIndex];
				leftWeight[index] += BALANCE_WEIGHT;
				leftWeight[index] += leftSupply[currentIndex];
				leftWeight[index] += rightSupply[currentIndex];
			}
		}
		
		if (rightWeight[index] == NOT_CALCULATED) {
			rightWeight[index] = 0;
			for (int j = 0; j < rightBalances[index].length; j++) {
				if (rightBalances[index][j] == NO_DATA)
					break;
				int currentIndex = rightBalances[index][j];
				if (leftWeight[currentIndex] == NOT_CALCULATED)
					calculate(currentIndex, leftBalances, rightBalances, leftWeight, rightWeight, leftSupply, rightSupply);
				if (rightWeight[currentIndex] == NOT_CALCULATED)
					calculate(currentIndex, leftBalances, rightBalances, leftWeight, rightWeight, leftSupply, rightSupply);
				rightWeight[index] += leftWeight[currentIndex];
				rightWeight[index] += rightWeight[currentIndex];
				rightWeight[index] += BALANCE_WEIGHT;
				rightWeight[index] += leftSupply[currentIndex];
				rightWeight[index] += rightSupply[currentIndex];
			}
		}
		
		if (leftWeight[index] > rightWeight[index])
			rightSupply[index] = leftWeight[index] - rightWeight[index];
		else
			leftSupply[index] = rightWeight[index] - leftWeight[index];
	}
	
	private static void output(int[] leftWeight, int[] rightWeight) {
		for (int j = 0; j < leftWeight.length; j++) {
			System.out.println(leftWeight[j] + " " + rightWeight[j]);
		}
	}
}
